package businessEntities;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class MainLogger {

    public static final Logger logger;

    static {
        
        logger = Logger.getLogger(MainLogger.class);
        
        File logsOutput = new File(FilesConfig.LOGS_OUTPUT_FILE_PATH);
        
        if (!logsOutput.exists()) {
            try {
                File logsDir = new File(FilesConfig.LOGS_OUTPUT_DIR);
                    boolean logDirExist = true;
                    if (!logsDir.isDirectory()) {
                        logDirExist = logsDir.mkdir();
                    }
                    if(logDirExist){
                        logsOutput.createNewFile();
                    }
                } catch (IOException e) {/*Empty*/
                }
        }
        else {
            try {
                    File archive= new File(FilesConfig.LOGS_OUTPUT_DIR + "\\runtime_archive_" + System.currentTimeMillis() + ".log");
                    FileUtils.copyFile(logsOutput, archive);
                    logsOutput.delete();
                    logsOutput = new File(FilesConfig.LOGS_OUTPUT_FILE_PATH);
                    logsOutput.createNewFile();
                } catch (IOException ex) { /*EMPTY*/
                }     
            }
        
        if (logsOutput.exists()) {
            PropertyConfigurator.configure(FilesConfig.LOGGER_CONFIG_URL);
        } else {
            BasicConfigurator.configure();
        }
    }       
}
